﻿// JScript 文件
function Mask()
{
    var div_back = document.getElementById("div_background");
    div_back.style.display = "block"; 
    var iframe_back = document.getElementById("iframe_back");
    iframe_back.style.display = "block";     
    var cart = document.getElementById("ShoppingCart");
    cart.style.display = "block";
    cart.style.position = "absolute";
    var bigImg = document.getElementById("Flag");
    var pleft = 0;
    var ptop = 0;
   
    var obj = bigImg.offsetParent;
 
    while(obj.tagName != "BODY")
    {
         pleft = pleft + obj.offsetLeft;
         ptop = ptop + obj.offsetTop;
         obj = obj.offsetParent;
    }
    ptop = ptop +50;
    cart.style.left = pleft +200+ "px";
    cart.style.top = ptop +"px";
    cart.style.width = 300 + "px";
    cart.style.backgroundColor  = "#ffffff";
}

function HidMask()
{
    var div_back = document.getElementById("div_background");
    div_back.style.display = "none"; 
    var iframe_back = document.getElementById("iframe_back");
    iframe_back.style.display = "none";   
    var cart = document.getElementById("ShoppingCart");
    cart.style.display = "none";    
}
function SetAddress(data)
{    
    document.getElementById("ShoppingCart").innerHTML=data;
}
function ModifyAction(id)
{debugger;
    Mask();
    SetAddress("<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>");  
     $j.post("../Handler/AddressManageHandler.ashx", {ID:id, OP:"model"},
            function(data){               
               var temp=data.split(',');
               document.getElementById("Hid_id").value=temp[0];
               document.getElementById("T_Name").value=temp[1];
               GetProvince(temp[2],temp[3],temp[4]);
               document.getElementById("Address").value=temp[5];
               document.getElementById("Post").value=temp[6];
               var phonearr=temp[7];
               phonearr=phonearr.split('-');
               
               if(phonearr.length==2)
               {
                    document.getElementById("Phone").value=phonearr[0];  
                    document.getElementById("Phone2").value=phonearr[1];                  
               }
               if(phonearr.length==3)
               {  
                    document.getElementById("Phone").value=phonearr[0];  
                    document.getElementById("Phone2").value=phonearr[1]; 
                    document.getElementById("Phone3").value=phonearr[2];                  
               }

               document.getElementById("Mobile").value=temp[8];
               document.getElementById("Detail").style.display="block";
               HidMask();
            }
        );
}
function GetProvince(Pid,Cid,Aid)
{
    $j.post("../Handler/AddressManageHandler.ashx", {OP:"Province"},
            function(data){
                var arr_option = data.split("&&");
                var sel = document.getElementById("Province");                  
                while(sel.length >0)
                {
                     sel.removeChild(sel.options(0));                    
                }
                for(var i = 0; i < arr_option.length-1;i++)
                {
                     eval("sel.options[i] = new Option" + arr_option[i] +";");
                }
                for(var j=0;j<sel.length ;j++)
                {
                    if(sel.options[j].value==Pid)
                    {
                        sel.selectedIndex=j;
                        break;
                    }
                }
                GetCity(Pid,Cid,Aid);
            }
        );
}
function GetCity(Pid,Cid,Aid)
{
    if(parseInt(Pid)>-1)
    {
    $j.post("../Handler/AddressHandler.ashx", {Province:Pid, OP:"City"},
                function(data){                                            
                   InitCity(data,Cid);
                   GetArea(Cid,Aid);
                }
            );
     }
}
function InitCity(data,value)
{
    var arr_option = data.split("&&");
    var sel = document.getElementById("City"); 
    while(sel.length >0)
    {
        sel.removeChild(sel.options(0));        
    }
    sel.options[0]=new Option("请选择","-1");
    for(var i = 1; i < arr_option.length;i++)
    {
        eval("sel.options[i] = new Option" + arr_option[i-1] +";");
    }
    if(value!=null)
    {        
        sel.value=value;
    }
    else
    {
        sel.value="-1";
    }
}
function GetArea(Cid,Aid)
{
    if(parseInt(Cid)>-1)
    {
     $j.post("../Handler/AddressHandler.ashx", {City:Cid, OP:"Area"},
                function(data){                          
                   InitArea(data,Aid);
                }
            );
     }
     else
     {
        
        var sel = document.getElementById("Area");
              
        while( sel.length>0 )
        {
           sel.removeChild(sel.options(0));            
        }
        sel.options[0]=new Option("请选择","-1");
        sel.value="-1";
     }
}
function InitArea(data,selectvalue)
{
    var arr_option = data.split("&&");
    var sel = document.getElementById("Area");      
    while(sel.length>0 )
    {
        sel.removeChild(sel.options(0));        
    }
    sel.options[0]=new Option("请选择","-1");
    for(var i = 1; i < arr_option.length;i++)
    {
        eval("sel.options[i] = new Option" + arr_option[i-1] +";");
    }
    if(selectvalue!=null)
    {
        sel.value=selectvalue;
    }
    else
    {
        sel.value="-1";
    }
}
function Save()
{debugger;
    if(CheckAddress())
    {
        var ID=document.getElementById("Hid_id").value;
        var reciver=document.getElementById("T_Name").value;
        var Province=document.getElementById("Province").value;
        var City=document.getElementById("City").value;
        var Area=document.getElementById("Area").value;
        var daddress=document.getElementById("Address").value;
        var post=document.getElementById("Post").value;
        
        var phone0=document.getElementById("Phone");//区号
        var phone1=document.getElementById("Phone2");//电话号码
        var phone2=document.getElementById("Phone3");//分机号码
    
        var phone="";
        if(phone1.value.length>0&&phone1.value!="号码")
        {
            phone=phone0.value+"-"+phone1.value;            
        }
        if(phone2.value.length>0&&phone2.value!="分机")
        {
            phone=phone0.value+"-"+phone1.value+"-"+phone2.value;
        }
        var mobile=document.getElementById("Mobile").value;
        Mask();
        SetAddress("<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>");        
        $j.post("../Handler/AddressManageHandler.ashx", {ID:ID,Reciver:reciver,Province:Province,City:City,Area:Area,Address:daddress,Post:post,Phone:phone,Mobile:mobile, OP:"Up"},
                function(data){     
                    if(data=="true")                     
                    {
                        window.location.reload();
                    }
                    else
                    {
                        alert("修改失败!");
                        HidMask();
                    }
                }
            );
    }
}
function Clear(obj)
{
    if(obj.value=="区号"||obj.value=="号码"||obj.value=="分机")
    {
        obj.value="";
    }
}

function CheckAddress()
{
    var reciver=document.getElementById("T_Name");
    if(reciver.value.length<1)
    {
        alert("收货人姓名不能为空");
        reciver.focus();
        return false;
    }
    var Province=document.getElementById("Province");    
    if(Province.selectedIndex<1)
    {
        alert("请选择省份");
        Province.focus();
        return false;
    }
    var City=document.getElementById("City");
    if(City.selectedIndex<1)
    {
        alert("请选择城市");
        City.focus();
        return false;
    }
    var Area=document.getElementById("Area");
    if(Area.selectedIndex<1)
    {
        alert("请选择区县");
        Area.focus();
        return false;
    }
    var daddress=document.getElementById("Address");
    if(daddress.value.length<1)
    {
        alert("请填写具体地址");
        daddress.focus();
        return false;
    }
    var post=document.getElementById("Post");
    if(isNaN(post.value))
    {
        alert("邮编格式错误,请检查您输入法的全角/半角状态！");
        post.focus();
        return false;
    }
    var regular=/^\d{6}$/;
    if(!regular.test(post.value))
    {
        alert("邮编不正确");
        post.focus();
        return false;
    }
    var phone=document.getElementById("Phone");//区号
    var phone1=document.getElementById("Phone2");//电话号码
    var phone2=document.getElementById("Phone3");//分机号码
    var mobile=document.getElementById("Mobile");
    if((phone1.value.length==0||phone1.value=="号码")&&mobile.value.length==0)
    {alert("手机或固定电话必填一个"); return false;}
    
    if(phone.value.length>0&&phone.value!="区号"||phone1.value.length>0&&phone1.value!="号码"||phone2.value.length>0&&phone2.value!="分机")
    {
    if((phone.value.length>0&&phone.value!="区号")||(phone1.value.length>0&&phone1.value!="号码"))
    {
        if(isNaN(phone.value)||isNaN(phone1.value))
        {
            alert("固定电话格式错误,请检查您输入法的全角/半角状态！");
            return false;
        }
        var reg=/^\d{3,4}$/;//检查区号
        var reg1=/^\d{7,8}$/;//检查电话号码
        if(!reg.test(phone.value)||!reg1.test(phone1.value))
        {
            alert("固定电话格式错误");
            return false;
        }
    }
    if(phone2.value.length>0&&phone2.value!="分机")
    {
        if(isNaN(phone.value)||isNaN(phone1.value)||isNaN(phone2.value))
        {
            alert("固定电话格式错误,请检查您输入法的全角/半角状态！");
            return false;
        }
        var reg=/^\d{3,4}$/;//检查区号
        var reg1=/^\d{7,8}$/;//检查电话号码
        var reg2=/^\d{1,5}$/;//检查分机号
        if(!reg.test(phone.value)||!reg1.test(phone1.value)||!reg2.test(phone2.value))
        {
            alert("固定电话格式错误");
            return false;
        }
    }
    }
    if(mobile.value.length>0)
    {
        if(isNaN(mobile.value))
        {
            alert("手机号码格式错误,请检查您输入法的全角/半角状态！");
            mobile.focus();
            return false;
        }
        var reg=/^\d+$/;//检查手机
        if(!reg.test(mobile.value))
        {
            alert("请输入正确的手机号码");
            mobile.focus();
            return false;
        }
    }
    return true;
}
function Delete(param)
{
    Mask();
    SetAddress("<br /><br />数据加载中......<br /><br /><img src='../images/ajax-loader.gif'>");
     $j.post("../Handler/AddressManageHandler.ashx", {ID:param,OP:"Delete"},
                function(data){     
                    if(data=="true")                     
                    {
                        window.location.reload();
                    }
                    else
                    {
                        alert("修改失败!");
                        HidMask();
                    }
                }
            );
}